package com.cloudlinkscm.base.authcenter.service.impl;

import com.cloudlinkscm.base.authcenter.dao.RoleDao;
import com.cloudlinkscm.base.authcenter.dao.RolePermissionRelationDao;
import com.cloudlinkscm.base.authcenter.domain.RoleManager;
import com.cloudlinkscm.base.authcenter.domain.RoleRelationTreeVO;
import com.cloudlinkscm.base.authcenter.exception.RoleNameRepeatException;
import com.cloudlinkscm.base.authcenter.exception.RoleNotFindException;
import com.cloudlinkscm.base.authcenter.model.Role;
import com.cloudlinkscm.base.authcenter.model.RolePermissionRelation;
import com.cloudlinkscm.base.authcenter.model.dto.RoleMoveDTO;
import com.cloudlinkscm.base.authcenter.model.dto.TenantRoleSaveDTO;
import com.cloudlinkscm.base.authcenter.model.dto.TenantRoleUpdateDTO;
import com.cloudlinkscm.base.authcenter.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by sin on 2017/8/8.
 *
 * 角色 service
 */
@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDao roleDao;
	@Autowired
	private RolePermissionRelationDao relationDao;
	@Autowired
	private RoleManager roleManager;


	/**
	 * 角色：租户下的角色
	 *
	 * @param tenantId 租户 id
	 * @return 租户下的角色
	 */
	@Override
	public List<Role> list(String tenantId) {
		return roleDao.selectByTenantId(tenantId);
	}


	/**
	 * 角色：保存
	 *
	 * @param saveDTO 需要保存的 role 信息
	 * @return 保存后的 角色
	 */
	@Override
	public Role save(TenantRoleSaveDTO saveDTO) {

		Role baseRole = roleDao.selectByRoleNameAndTenantId(saveDTO.getRoleName());
		if (baseRole != null) {
			// role name repeat
			throw new RoleNameRepeatException();
		}
		return roleManager.save(saveDTO);
	}

	/**
	 * 角色：更新
	 *
	 * @param updateDTO 需要更新的信息
	 * @return 更新受影响的条数
	 */
	@Override
	public int update(TenantRoleUpdateDTO updateDTO) {

		Role baseRole = roleDao.selectByRoleNameAndTenantId(updateDTO.getRoleName());
		if (baseRole != null) {
			// role name repeat
			throw new RoleNameRepeatException();
		}

		return roleManager.update(updateDTO);
	}


	/**
	 * 角色：激活
	 *
	 * @param id 角色 id
	 * @return 更新受影响的条数
	 */
	@Override
	public int active(String id) {
		Role role = roleDao.selectByPrimaryKey(id);
		if (role == null) {
			// role is null
			throw new RoleNotFindException();
		}

		return roleManager.active(id);
	}

	/**
	 * 角色：失效
	 *
	 * @param id 角色 id
	 * @return 更新受影响的条数
	 */
	@Override
	public int invalid(String id) {
		Role role = roleDao.selectByPrimaryKey(id);
		if (role == null) {
			// role is null
			throw new RoleNotFindException();
		}

		return roleManager.invalid(id);
	}


	///
	/// 角色权限分配

	/**
	 * 角色关系：relation 移动
	 *
	 * @param roleId 角色 id
	 * @param tenantId 租户 id
	 * @param moveDTO 需要移动的节点信息
	 * @return 更新受影响的条数
	 */
	@Override
	public int roleRelationMove(String roleId, String tenantId, RoleMoveDTO moveDTO) {

		if (roleDao.selectByPrimaryKey(roleId) == null) {
			// role not find
			throw new RoleNotFindException();
		}

		List<RolePermissionRelation> baseRolePermissionRelations =
				relationDao.selectByRoleIdAndResourceIds(roleId, moveDTO.getNodeIds());

//		if (CollectionUtils.isEmpty(baseRolePermissionRelations)) {
			// is exists
//			throw new RoleRelationIsExistsException();
//		}

		// move in
		if (moveDTO.getMoveMode().equals(RoleMoveDTO.MOVE_MODE_IN)) {
			return roleManager.roleRelationAdd(roleId, moveDTO.getNodeIds(), tenantId);
		}
		else {
			// move out
			return roleManager.roleRelationRemove(moveDTO.getNodeIds());
		}
	}


	/**
	 * 角色关系：tree
	 *
	 * <p>
	 *     构建一个 {@link RoleRelationTreeVO}
	 *     返回两个属性，{@link RoleRelationTreeVO#distribution} 已分配资源
	 *     另一个，{@link RoleRelationTreeVO#notDistribution} 未分配资源
	 * </p>
	 *
	 * @param roleId 角色 id
	 * @return 构建的 tree
	 */
	@Override
	public RoleRelationTreeVO roleRelationTree(String roleId) {

		if (roleDao.selectByPrimaryKey(roleId) == null) {
			throw new RoleNotFindException();
		}
		return roleManager.tree(roleId);
	}

}
